package com.ztjava.ask.controller;

import java.util.Map;

import com.jfinal.aop.Duang;
import com.ztjava.ask.model.AskCatalog;
import com.ztjava.ask.service.AskService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.client.member.FrontClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
@ControllerClass(client=FrontClient.class,path="/ask")
@UserController(portal=Constant.P_MEMBER)
public class AskController extends AskBaseController{

	private static AskService askService = Duang.duang(AskService.class);
	
	@UserMethod(option="ASK_AUTH")
	public void index(){
		Map<String, String[]> params = getParaMapCustom();
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		setAttr("pd",askService.getQuestionPage(client, params));
		setAttr("subCatalog", askService.catalogList(params));
		render("index.html");
	}

	@UserMethod(option="ASK_AUTH")
	public void list(){
		keepPara();
		Map<String, String[]> params = getParaMap();
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id==0){
			String custom = getPara(0,"");
			if(!custom.equals("")){
				AskCatalog askCatalog = askService.getCatalog(custom);
				if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
					renderText("目录不存在");
					return;
				}
				catalog_id = askCatalog.getInt("id");
			}
		}
		AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
		if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
			renderText("目录不存在");
			return;
		}	
		setAttr("askCatalog", askCatalog);
		params.put("num", new String[]{askCatalog.getStr("num")});

		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		setAttr("pd",askService.getQuestionPage(client, params));
		setAttr("catalogPath", askService.catalogPath(askCatalog.getStr("num")));
		
		setAttr("subCatalogList", askService.getSubCatalogList(catalog_id));
		render(askCatalog.getStr("list_tpl"));
	}
	


}
